<?php

class Core_Model_Subject_Collection_Content extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('subject/content');
        $this->from('{t}subject_content', 'a')
                ->field('a.*')
                ->neq('a.status', "'deleted'");
    }

    public function joinAccount() {
        $this->join()->inner('{t}permission_account', 'c')->on('a.mender_id', 'c.id')
                ->closest()
                ->select()->field('c.full_name', 'mender_name');
        return $this;
    }

    public function joinCategory() {
        $this->join()->inner('{t}subject_category', 'f')->on('a.category_id', 'f.id')
                ->closest()
                ->select()->field('f.name', 'category_name');
        return $this;
    }

    public function joinTemplate() {
        $this->join()->left('{t}website_template', 'd')->on('a.template_id', 'd.id')
                ->closest()
                ->select()->field('d.name', 'template_name');
        return $this;
    }

    public function joinAccountRelation(){
        $this->innerJoin('{t}subject_account','g','a.id','g.subject_id')->field('g.account_id','account_id');
        return $this;
    }

//    public function joinRelation() {
//        return $this->innerJoin('{t}subject_industry', 'e', 'e.subject_id', 'a.id')
//                ->field('e.industry_id', 'industry_id');
//    }

    public function joinIndustry() {
        return $this->innerJoin('{t}subject_industry', 'e', 'e.subject_id', 'a.id')
                ->field('e.industry_id', 'industry_id');
    }

    public function sortBy($name, $dir) {
        switch ($name) {
            case 'name':
                return $dir == 'asc' ? $this->asc("a.name") : $this->desc("a.name");
            case 'modifiedtime':
                return $dir == 'asc' ? $this->asc("a.modified_time") : $this->desc("a.modified_time");
            case 'hot':
                return $dir == 'asc' ? $this->asc("a.is_hot") : $this->desc("a.is_hot");
            case 'new':
                return $dir == 'asc' ? $this->asc("a.is_new") : $this->desc("a.is_new");
            case 'top':
                return $dir == 'asc' ? $this->asc("a.is_top") : $this->desc("a.is_top");
            case 'clickcount':
                return $dir == 'asc' ? $this->asc("a.click_count") : $this->desc("a.click_count");
        }
        return parent::sortBy($name, $dir);
    }

    public function filterBy($name, $arg) {
        switch ($name) {
            case 'id':
                return $this->eq('a.id', ':id:i')->addParameter('id', $arg);
            case 'categoryid':
                return $this->eq('a.category_id', ':category_id:i')->addParameter('category_id', $arg);
            case 'name':
                return $this->like('a.name', ':name:s')->addParameter('name', "%$arg%");
            case 'status':
                return $this->eq('a.status', ':status:s')->addParameter('status', $arg);
            case 'top':
                return $this->eq('a.is_top', $arg);
            case 'new':
                return $this->eq('a.is_new', $arg);
            case 'hot':
                return $this->eq('a.is_hot', $arg);
            case 'tag':
                $tagNames = explode(' ', $arg);
                for ($i = 0; $i < count($tagNames); $i++) {
                    if ($i > 0) {
                        $this->orNext();
                    }
                    $this->like('a.tag', "'%{$tagNames[$i]}%'");
                }
                return $this;
            case 'status':
                return $this->eq('a.status', ':status:s')->addParameter('status', $arg);
            case 'ids':
                return $this->contain('a.id', ':ids:i')->addParameter('ids', $arg);
            case 'accountid':
                $this->eq('g.account_id', ':account_id:i')->addParameter('account_id', $arg);
                break;
        }
        return parent::filterBy($name, $arg);
    }

    public function filterByRole($id) {
        $ids = is_array($id) ? $id : array($id);
        $this->where()
                ->inQuery('a.creator_id')
                ->from('{t}permission_account_role')
                ->where()
                ->contain('role_id', ':roleIds:i')
                ->closest()
                ->addParameter('roleIds', $ids)
                ->select()->field('account_id');
        return $this;
    }

}